package com.eastfair.home.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.home.dao.ExhibitionManageNoticeMapper;
import com.eastfair.home.entity.ExhibitionManageNotice;
import com.eastfair.home.service.ExhibitionManageNoticeService;
import com.eastfair.boot.service.SuperServiceImpl;

import com.eastfair.venueservice.entity.ExhibitionManage;
import com.eastfair.venueservice.entity.WorkOrderServiceItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;

import java.util.Collection;

//import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.aop.framework.AopContext;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.annotation.annotation.cache.Cache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
//import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.eastfair.home.vo.*;
import com.eastfair.home.dto.*;

/**
 * <p>
 * 业务实现类
 * 展会须知
 * </p>
 *
 * @author linan
 * @date 2022-10-22
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ExhibitionManageNoticeServiceImpl extends SuperServiceImpl<ExhibitionManageNoticeMapper, ExhibitionManageNotice> implements ExhibitionManageNoticeService {


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ExhibitionManageNotice> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }


    @Override
    protected R<Boolean> handlerSave(ExhibitionManageNotice model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    /**
     * 通用查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    public List<ExhibitionManageNotice> list(ExhibitionManageNoticeDTO modelDTO, List<Long> ids) {
        return ((ExhibitionManageNoticeService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
            magic = @Magic(
                    key = "{" +
                            "{'isEnabled', #args[0]?.isEnabled}," +
                            "{'exhibitionManageId', #args[0]?.exhibitionManageId}," +
                            "{'notice', #args[0]?.notice}," +
                            "{'id', #args[1]}" +
                            "}"))
    public List<ExhibitionManageNotice> listOfCache(ExhibitionManageNoticeDTO modelDTO, List<Long> ids) {
        //查询
        QueryWrapper<ExhibitionManageNotice> queryWrapper = new QueryWrapper<>();
        if (modelDTO != null) {
            queryWrapper.lambda().eq(modelDTO.getExhibitionManageId() != null, ExhibitionManageNotice::getExhibitionManageId, modelDTO.getExhibitionManageId());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getNotice()), ExhibitionManageNotice::getNotice, modelDTO.getNotice());
        }
        if (ids != null && !ids.isEmpty()) {
            queryWrapper.lambda().in(ExhibitionManageNotice::getId, ids);
        }
        queryWrapper.lambda().eq(ExhibitionManageNotice::getIsDeleted, BusinessConstant.DELETE_NO);
        List<ExhibitionManageNotice> modelList = list(queryWrapper);
        return modelList;
    }

    @Override
    public Boolean updateExhibitionNotice(ExhibitionManageNoticeDTO params) {
        ExhibitionManageNotice one = getOne(new QueryWrapper<ExhibitionManageNotice>().lambda()
                .eq(ExhibitionManageNotice::getExhibitionManageId, params.getExhibitionManageId())
                .eq(ExhibitionManageNotice::getIsDeleted, BusinessConstant.NO));
        if(Objects.nonNull(one)){
            return update(new UpdateWrapper<ExhibitionManageNotice>().lambda()
                    .eq(ExhibitionManageNotice::getExhibitionManageId, params.getExhibitionManageId())
                    .eq(ExhibitionManageNotice::getIsDeleted, BusinessConstant.NO).set(ExhibitionManageNotice::getNotice,params.getNotice()));
        }else {
            ExhibitionManageNotice exhibitionManageNotice = BeanUtil.copyProperties(params, ExhibitionManageNotice.class);
            return save(exhibitionManageNotice);
        }
    }

    @Override
    public ExhibitionManageNoticeVO queryExhibitionNotice(ExhibitionManageNoticePageQuery params) {
        ExhibitionManageNotice one = getOne(new QueryWrapper<ExhibitionManageNotice>().lambda()
                .eq(ExhibitionManageNotice::getExhibitionManageId, params.getExhibitionManageId())
                .eq(ExhibitionManageNotice::getIsDeleted, BusinessConstant.NO));
        return BeanUtil.copyProperties(one,ExhibitionManageNoticeVO.class);
    }


    /**
     * do转vo
     *
     * @param model model
     * @return modelVo
     */
    @Override
    public ExhibitionManageNoticeVO doToVo(ExhibitionManageNotice model) {
        ExhibitionManageNoticeVO modelVo = new ExhibitionManageNoticeVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }

    /**
     * do转vo
     *
     * @param modelList modelList
     * @return list
     */
    @Override
    public List<ExhibitionManageNoticeVO> doToVos(List<ExhibitionManageNotice> modelList) {
        if (modelList == null || modelList.isEmpty()) {
            return null;
        }
        List<ExhibitionManageNoticeVO> result = new ArrayList<>();
        for (ExhibitionManageNotice model : modelList) {
            result.add(doToVo(model));
        }
        return result;
    }
}
